using System;
using System.Collections.Generic;
using System.Linq;
using Survival.Services;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Collections;


namespace Survival.Plants
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public partial class PlantManager : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch plantBatch;
        Dictionary<string, Texture2D> spriteDictionary;
        List<Plant> plantList;
        PlantServices plantServices;
        IInputService inputService;
        IDebugTextService debugTextService;
        IWorldQuery worldQeueryService;
        Random rand = new Random();

        public List<Plant> PlantList { get { return plantList; } }
        
        public PlantManager(Game game)
            : base(game)
        {
            spriteDictionary = new Dictionary<string, Texture2D>();
        }

        public void AddPlant(Plant plant)
        {
            plantList.Add(plant);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
            plantBatch = new SpriteBatch(GraphicsDevice);
            plantList = new List<Plant>();
            inputService = (IInputService)Game.Services.GetService(typeof(IInputService));
            debugTextService = (IDebugTextService)Game.Services.GetService(typeof(IDebugTextService));
            worldQeueryService = (IWorldQuery)Game.Services.GetService(typeof(IWorldQuery));
            plantServices = new PlantServices(debugTextService, inputService, worldQeueryService);
            AddPlant(new TallGrass(Vector2.One,plantServices));
        }

        protected override void LoadContent()
        {
            spriteDictionary.Add("TallGrass", Game.Content.Load<Texture2D>("TallGrass"));
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (inputService.KeyPressed(Keys.G))
                AddPlant(new TallGrass(new Vector2(rand.Next(800), rand.Next(500)), plantServices));

            foreach (Plant plant in plantList)
            {
                plant.Update(gameTime);
            }

            Vector2 plantCountPosition = new Vector2(1,40);
            debugTextService.DrawString(new DrawableString("Plants Count: " + plantList.Count, plantCountPosition));
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Texture2D currentSprite;

            plantBatch.Begin();
            foreach (Plant plant in plantList)
            {
                //todo: remove new vector if it's faster
                spriteDictionary.TryGetValue(plant.SpriteName, out currentSprite);
                Vector2 spriteCenter = new Vector2(currentSprite.Bounds.Center.X, currentSprite.Bounds.Center.Y);
                plantBatch.Draw(currentSprite, plant.Position,null, Color.White,0,spriteCenter,plant.Scale,SpriteEffects.None,0);
            }
            plantBatch.End();

            base.Draw(gameTime);
        }
    }

    public class PlantServices
    {
        private IWorldQuery worldQuery;
        private IInputService inputService;
        private IDebugTextService debugTextService;

        public IWorldQuery WorldQuery { get { return worldQuery; } }
        public IInputService InputService { get { return inputService; } }
        public IDebugTextService DebugTextService { get { return debugTextService; } }

        public PlantServices(IDebugTextService debugtext, IInputService input, IWorldQuery worldQuery)
        {
            debugTextService = debugtext;
            inputService = input;
            this.worldQuery = worldQuery;
        }
    }
}
